package com.example.parallelandroid;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

public class DataListAdapter extends BaseAdapter {

    private Context mContext;
    private List<Data> datas = new ArrayList<>();
    private int layoutResId;
    private SharedPreferences sharedPreferences;

    public DataListAdapter(Context mContext, List<Data> datas,
                           int layoutResId) {
        this.mContext = mContext;
        this.datas = datas;
        this.layoutResId = layoutResId;
    }

    @Override
    public int getCount() {
        if (datas != null){
            return datas.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (datas != null){
            return datas.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {//header
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null){
            sharedPreferences= mContext.getSharedPreferences("user", Context.MODE_PRIVATE);
            //加载item的布局文件，生成一个View对象，再给converView赋值
            convertView = LayoutInflater.from(mContext).inflate(layoutResId,null);
            //获取每一个item中的控件对象
            viewHolder = new ViewHolder();
            viewHolder.tvRank=convertView.findViewById(R.id.tv_rank);
            viewHolder.tvHead=convertView.findViewById(R.id.tv_head);
            viewHolder.tvNickName=convertView.findViewById(R.id.tv_nick_name);
            viewHolder.tvProvince=convertView.findViewById(R.id.tv_province);
            viewHolder.tvGrade=convertView.findViewById(R.id.tv_grade);
            //将ViewHolder对象同converView关联起来
            convertView.setTag(viewHolder);
        }else {//重复利用convertView对象
            //获取到跟当前的convertView绑定在一起的ViewHolder对象
            viewHolder = (ViewHolder) convertView.getTag();
        }
        String avatar=datas.get(position).getAvatar();
        viewHolder.tvRank.setText((getItemId(position)+1)+"");
        switch (avatar){
            case "1":
                Glide.with(mContext).load(R.mipmap.tou1).into(viewHolder.tvHead);
                break;
            case "2":
                Glide.with(mContext).load(R.mipmap.tou2).into(viewHolder.tvHead);
                break;
            case "3":
                Glide.with(mContext).load(R.mipmap.tou3).into(viewHolder.tvHead);
                break;
            case "4":
                Glide.with(mContext).load(R.mipmap.tou4).into(viewHolder.tvHead);
                break;
            case "5":
                Glide.with(mContext).load(R.mipmap.tou5).into(viewHolder.tvHead);
                break;
            case "6":
                Glide.with(mContext).load(R.mipmap.tou6).into(viewHolder.tvHead);
                break;
            case "7":
                Glide.with(mContext).load(R.mipmap.tou7).into(viewHolder.tvHead);
                break;
            case "8":
                Glide.with(mContext).load(R.mipmap.tou8).into(viewHolder.tvHead);
                break;
            case "9":
                Glide.with(mContext).load(R.mipmap.tou9).into(viewHolder.tvHead);
                break;
            case "10":
                Glide.with(mContext).load(R.mipmap.tou10).into(viewHolder.tvHead);
                break;
            case "11":
                Glide.with(mContext).load(R.mipmap.tou11).into(viewHolder.tvHead);
                break;
            case "12":
                Glide.with(mContext).load(R.mipmap.tou12).into(viewHolder.tvHead);
                break;

        }
        viewHolder.tvNickName.setText(datas.get(position).getNickName());
        for(int i=0;i<datas.size();i++){
            if(viewHolder.tvNickName.getCurrentTextColor()==Color.RED){
                viewHolder.tvNickName.setTextColor(Color.GRAY);
            }
        }
        if(datas.get(position).getUserId().equals(sharedPreferences.getString("userId",""))){
            viewHolder.tvNickName.setTextColor(Color.RED);
        }
        if(datas.get(position).getProvince()==null){
            viewHolder.tvProvince.setText("未知");
        }else{
            viewHolder.tvProvince.setText(datas.get(position).getProvince());
        }
        switch(Constant.spinnerContent){
            case "综合榜":
                viewHolder.tvGrade.setText(datas.get(position).getComprehensive()+"");
                break;
            case "智力榜":
                viewHolder.tvGrade.setText(datas.get(position).getIntelligence()+"");
                break;
            case "体力榜":
                viewHolder.tvGrade.setText(datas.get(position).getPower()+"");
                break;
            case "魅力榜":
                viewHolder.tvGrade.setText(datas.get(position).getCharm()+"");
                break;
        }
        //给控件对象绑定要显示的数据
        return convertView;
    }

    private static class ViewHolder{
        //每一个item中的控件
        private TextView tvRank,tvNickName,tvGrade,tvProvince;
        private ImageView tvHead;
    }
}
